209.k8s中srs的各個pod metrics取得
事由
同事正在弄SRS串流,照我以前的寫法,
是透過service去取得單一個pod的metrics,
這樣只會有一個pod的資料,
所以要想辦法取得所有pod的資料。
正文
由於我有套用istio,
於是有套istio-proxy的pod通通會預設塞
prometheus.io/path: /stats/prometheus
prometheus.io/port: '15020'
prometheus.io/scrape: 'true'
所以直接在deploy加參數進去時,也會被改寫。
於是只好額外寫job 如下
- job_name: 'srs'
scrape_interval: 10s
static_configs:
- targets:
- 'srs.default.svc.cluster.local:9972'
呼叫 service 的 srs ,然後去取得metrics的metrics。
但當有多個pod的時候,這就會出問題了。
因為service到pod的流量平均分配,你不會知道他連去哪個pod。
但今天我要抓全部metrics,這種寫法就會出問題。
其中 static_configs
這個只適用於 固定字串,
所以對於會變來變去的pod ip沒有用。
於是要改成
- job_name: 'srs'
scrape_interval: 10s
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_name]
action: keep
regex: srs-.*
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
replacement: /metrics # 強制指定路徑為 /metrics
- source_labels: [__meta_kubernetes_pod_container_port_name]
action: replace
target_label: portname
replacement: 9972
規則由上往下依序執行,有符合才會往下。
kubernetes_sd_configs
有這5種方式(GPT抄來的)
-
role: endpoints:
- 這個組態項告訴 Prometheus 去發現 Kubernetes 中的端點對象。端點對象代表了服務的後端 Pod 的 IP 地址和連接埠。
- 通過此組態項,Prometheus 可以直接與後端 Pod 進行通訊,收集指標資料。
-
role: service:
- 這個組態項告訴 Prometheus 去發現 Kubernetes 中的服務對象。
- Prometheus 將會為每個服務發現服務的所有後端 Pod,並從每個 Pod 中抓取指標資料。
-
role: pod:
- 這個組態項告訴 Prometheus 去發現 Kubernetes 中的 Pod 對象。
- Prometheus 將會為每個發現的 Pod 收集指標資料。
-
role: ingress:
- 這個組態項告訴 Prometheus 去發現 Kubernetes 中的 Ingress 對象。
- Prometheus 將會監視 Ingress 對象,並收集與之相關的指標資料。
-
role: node:
- 這個組態項告訴 Prometheus 去發現 Kubernetes 中的節點對象。
- Prometheus 將會為每個節點收集相關的指標資料。
再來是重點 relabel_configs
分別有
- source_labels:來源標籤,可以參考prometheus的UI,上面有顯示很多label
- action:四種動作(keep,drop,replace,labelmap)。
決定你要將值保留、終止、取代或標籤對應 - target_label:目的標籤,
抓資料的url,由下面三種特殊標籤組成__scheme__
+__address__
+__metrics_path__
。
範例:https://192.168.103.224:9527/metrics
主要是決定特殊標籤要用什麼,不然隨便取名都可以。 - regex,當作是條件判斷這裡面的值要符合規則能往下。
下面regex意思是,pod name要符合srs-.*
的才保留。
- source_labels: [__meta_kubernetes_pod_name]
action: keep
regex: srs-.*
- replacement:變更的值
ref.